<?php
namespace app\common\model\users;

use think\Model;
use think\Db;

/*************************************************
 *ClassName:     UsersMoney
 *Description:   用户账户模型
 *Others:
 *************************************************/
class UsersMoney extends Model
{
    /**
     * getBalance 计算余额
     * 用户账户增加资金的总额减去减少的总额
     * @xiao
     * @DateTime 2016-11-27T21:24:15+0800
     * @param    int $uid 用户uid
     * @return   array     [error_code, error_msg, balance]
     */
    static public function getBalance($uid)
    {
        $where = array('uid' => $uid);
        $income = 0;
        $expense = 0;
        $res = Db::name('users_money')->where($where)->select();
        if ($res) {
            foreach ($res as $key => $v) {
                $income += $v['income'];
                $expense += $v['expense'];
            }
            $balance = sprintf("%.2f",  $income - $expense );
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['balance'] = $balance;
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = '查询失败';
        }
        return $result;
    }

    /**
     * addIncome 增加用户收入记录
     * @xiao
     * @DateTime 2016-11-27T21:35:49+0800
     * @param    int $uid
     * @param    array $data ['des' => , 'type' => , `income` => , `order_id` => ]
     * @return   array     [error_code, error_msg, balance]
     */
    static public function addIncome($data)
    {
        $res = Db::name('users_money')->insertGetId($data);
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $row = self::getBalance($data['uid']);
            $balance['balance'] = $row['balance'];
            Db::name('users_money')->where(['id' => $res])->update($balance);
            //这里增加了用户收入记录后应该更新users用户表中的账户余额
            $result['balance'] = $row['balance'];
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = '添加用户收入失败';
        }
        return $result;
    }

    /**
     * addExpense 增加用户支出记录
     * @xiao
     * @DateTime 2016-11-27T21:35:49+0800
     * @param    int $uid
     * @param    array $data ['des' => , 'type' => , `income` => , `order_id` => ]
     * @return   array     [error_code, error_msg, balance]
     */
    static public function addExpense($data)
    {
        $res = Db::name('users_money')->insertGetId($data);
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $row = self::getBalance($data['uid']);
            $balance['balance'] = $row['balance'];
            Db::name('users_money')->where(['id' => $res])->update($balance);
            //这里增加用户支出记录后应该更新users用户表中的账户余额
            $result['balance'] = $row['balance'];
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = '添加失败';
        }
        return $result;
    }

    /**
     * getSystemBalance 平台全部收入条数
     * @liangjianzhao karl
     * @param    array     $where 查询条件
     * @return   array     [error_code, error_msg, balance]
     * @DateTime 2016-11-22T20:46:59+0800
     */
    static public function getSystemBalance($where = [])
    {
        $result = ['error_code' => 0, 'error_msg' => ''];
        $system_income = Db::name('users_money')->where($where)->sum('income');
        $system_expense = Db::name('users_money')->where($where)->sum('expense');
        $result['balance'] = $balance = sprintf("%.2f", $system_income - $system_expense);
        return $result;
    }

    /**
     * getRows 查询多条数据
     * @xiao karl
     * @param    array                   $where 查询条件
     * @param    integer                 $page  当前页
     * @param    string                  
     * @param    array                   $order  排序条件
     * @param    array                   $url   查询条件
     * @param    integer                 $page_num   每页显示数量
     * @return   array                   [error_code, error_msg, data=> [] ]
     */
    static public function getRowsPage($where = [], $page = "", $sort='id', $order='desc', $url = [] ,$page_num=20)
    {   
        $class_name = get_called_class();
        if (empty($page))
            $res = Db::name($class_name::$table_name)->where($where)->order($sort.' '.$order)->paginate($page_num, false , ['query' => $url]);
        else
            $res = Db::name($class_name::$table_name)->where($where)->order($sort.' '.$order)->page($page,$page_num)->select();
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $res;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '查询失败';
        }
        return $result;
    }    

}

?>